home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / MAIN.C < prev    next >
C/C++ Source or Header  |  1992-03-17  |  9KB  |  261 lines

  1. /*
  2.    Module:  main.c
  3.    Date:    3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file ADI7221.DOC.
  15.  
  16.    Purpose: This module provides the main processing routine for the generic
  17.             ADI driver.  This handles program flow, and determines which of
  18.             the device specific procedures to call based on the ADI code
  19.             found in the source file.
  20.  
  21.             To effectively use this module, it should be compiled and linked
  22.             with a set of obj files that provide the following functions:
  23.  
  24.                 int dev_beginplot(PLTFILE *outfile)
  25.                 int dev_endplot(PLTFILE *outfile)
  26.                 int dev_move(unsigned xcoord,ycoord; PLTFILE *outfile)
  27.                 int dev_draw(unsigned xcoord,ycoord; PLTFILE *outfile)
  28.                 int dev_newpen(unsigned pennum; PLTFILE *outfile)
  29.                    Note: dev_newpen should initiate a hardware autoselect
  30.                 int dev_setspeed(unsigned speed; PLTFILE *outfile)
  31.                 int dev_linetype(unsigned linetype; PLTFILE *outfile)
  32.                 int dev_penchange(PLTFILE *outfile)
  33.                    Note: The main program calls dev_penchange and then pauses
  34.                          for a manual pen change.  Since this version is not
  35.                          a direct feed to the device, dev_penchange will
  36.                          never be called.  However, future versions may use
  37.                          this function.
  38.                 int dev_abortplot(PLTFILE *outfile)
  39.  
  40.             Each of these functions must produce a char array of codes
  41.             and output those codes via the putarr function.  The putarr 
  42.             function and the PLTFILE data type are part of the inout module.
  43.  
  44.             Each function should return an int value indicating it's success
  45.             or failure.  Values returned should be as follows:
  46.  
  47.                 TRUE   - args (if any) converted successfully, no error was
  48.                          returned by putarr()
  49.                 BADIO  - putarr returned with an error
  50.                 BADFMT - could not convert arguments to device format
  51.  
  52.             The #defines for the codes listed above can be found in
  53.             retcodes.h.
  54.  
  55.             The device dependant code should also include a #define for
  56.             DEV_WELCOME.  This should be a set of arguments for printf
  57.             that will be printed at program startup.
  58.  
  59.    Functions Provided:
  60.  
  61.         main
  62.  
  63.    Functions Required:
  64.  
  65.         (the following functions are part of the generic package)
  66.         openin
  67.         openout
  68.         closein
  69.         closeout
  70.         getstr
  71.         parse
  72.         terminate
  73.  
  74.         (the following functions are device dependant)
  75.         dev_beginplot
  76.         dev_endplot
  77.         dev_move
  78.         dev_draw
  79.         dev_newpen      (device autoselect)
  80.         dev_newspeed
  81.         dev_linetype
  82.         (dev_penchange is not used by this version)
  83.         dev_abortplot
  84. */
  85.  
  86. #include <stdio.h>
  87. #include <stdlib.h>
  88. #include "adicodes.h"
  89. #include "retcodes.h"
  90. #include "inout.h"
  91. #include "parse.h"
  92. #include "terminat.h"
  93. #include "hp7221.h"
  94.  
  95. #define MAXLINE 80  /* maximum line length in input file */
  96.  
  97. main(argc,argv)
  98.    int argc;
  99.    char *argv[];
  100. {
  101.    char line[MAXLINE+1];
  102.    int done, cnt, cmd;
  103.    unsigned arg1, arg2;
  104.    PLTFILE *infile = (PLTFILE *)NULL, *outfile = (PLTFILE *)NULL;
  105.  
  106.  
  107.    /* print welcome message */
  108.    printf (DEV_WELCOME);
  109.  
  110.    if (argc != 3) {
  111.        fprintf (stderr, "Usage: %s infile outfile\n",argv[0]);
  112.        exit (BADARGS);
  113.    }
  114.  
  115.    /* From this point on, program termination should be accomplished via
  116.       terminate()
  117.    */
  118.  
  119.    switch (openin(argv[1], &infile)) {
  120.       case TRUE:
  121.          break;
  122.       case NOBUFF:
  123.          terminate (BADIO, &infile, &outfile, "can't allocate input buffer");
  124.          break;
  125.       case BADOPEN:
  126.          terminate (BADIO, &infile, &outfile,
  127.                     "can't open input file for reading");
  128.          break;
  129.    }
  130.    switch (openout(argv[2], &outfile)) {
  131.       case TRUE:
  132.          break;
  133.       case NOBUFF:
  134.          terminate (BADIO, &infile, &outfile, "can't allocate output buffer");
  135.          break;
  136.       case BADOPEN:
  137.          terminate (BADIO, &infile, &outfile,
  138.                     "can't open output file for writing");
  139.          break;
  140.    }
  141.  
  142.    if (getstr(infile,MAXLINE,line) != 4) {
  143.       terminate (NOTADI, &infile, &outfile, "input file not a valid ADI file");
  144.    } else if (line[0]-'0' != BEGIN_PLOT) {
  145.       terminate (NOTADI, &infile, &outfile, "input file not a valid ADI file");
  146.    } else if (line[2]-'0' != ADIVERSION) {
  147.       fprintf (stderr, "Warning: input file not a version 1 ADI file.\n");
  148.    }
  149.    if (!dev_beginplot(outfile))
  150.       terminate (BADIO, &infile, &outfile, "I/O error on output");
  151.  
  152.    done = FALSE;
  153.    while  (!done && ((cnt = getstr(infile, MAXLINE, line)) > 0)) {
  154.       parse(line,&cmd,&arg1,&arg2);
  155.       switch(cmd) {
  156.          case BEGIN_PLOT:
  157.             terminate (BADCMD, &infile, &outfile,
  158.                        "Multiple BEGIN_PLOT commands found");
  159.             break;
  160.          case END_PLOT:
  161.             if (!dev_endplot(outfile))
  162.                terminate (BADIO, &infile, &outfile, "I/O error on output");
  163.             else
  164.                done=TRUE;
  165.             break;
  166.          case MOVE:
  167.             switch (dev_move(arg1,arg2,outfile)) {
  168.                case TRUE:
  169.                   break;
  170.                case BADIO:
  171.                   terminate (BADIO, &infile, &outfile, "I/O error on output");
  172.                   break;
  173.                case BADFMT:
  174.                   terminate (BADCMD, &infile, &outfile, "invalid MOVE command");
  175.                   break;
  176.             }
  177.             break;
  178.          case DRAW:
  179.             switch (dev_draw(arg1,arg2,outfile)) {
  180.                case TRUE:
  181.                   break;
  182.                case BADIO:
  183.                   terminate (BADIO, &infile, &outfile, "I/O error on output");
  184.                   break;
  185.                case BADFMT:
  186.                   terminate (BADCMD, &infile, &outfile, "invalid DRAW command");
  187.                   break;
  188.             }
  189.             break;
  190.          case NEW_PEN:
  191.             switch (dev_newpen(arg1,outfile)) {
  192.                case TRUE:
  193.                   break;
  194.                case BADIO:
  195.                   terminate (BADIO, &infile, &outfile, "I/O error on output");
  196.                   break;
  197.                case BADFMT:
  198.                   terminate (BADCMD, &infile, &outfile,
  199.                              "invalid NEW_PEN command");
  200.                   break;
  201.             }
  202.             break;
  203.          case SET_SPEED:
  204.             switch (dev_setspeed(arg1,outfile)) {
  205.                case TRUE:
  206.                   break;
  207.                case BADIO:
  208.                   terminate (BADIO, &infile, &outfile, "I/O error on output");
  209.                   break;
  210.                case BADFMT:
  211.                   terminate (BADCMD, &infile, &outfile,
  212.                              "invalid SET_SPEED command");
  213.                   break;
  214.             }
  215.             break;
  216.          case LINE_TYPE:
  217.             switch (dev_linetype(arg1,outfile)) {
  218.                case TRUE:
  219.                   break;
  220.                case BADIO:
  221.                   terminate (BADIO, &infile, &outfile, "I/O error on output");
  222.                   break;
  223.                case BADFMT:
  224.                   terminate (BADCMD, &infile, &outfile,
  225.                              "invalid LINE_TYPE command");
  226.                   break;
  227.             }
  228.             break;
  229.          case PEN_CHANGE:
  230. #ifdef WARN_PENCHANGE
  231.             fprintf (stderr, "Warning: PEN_CHANGE encountered but not supported.\n");
  232. #endif
  233.             break;
  234.          case ABORT_PLOT:
  235.             if (dev_abortplot(outfile))
  236.                terminate (ABORTED, &infile, &outfile,
  237.                           "input file contains ABORT_PLOT command");
  238.             else
  239.                terminate (BADIO, &infile, &outfile, "I/O error on output");
  240.             break;
  241.          default:
  242.             terminate (BADCMD, &infile, &outfile,
  243.                        "unknown command encountered");
  244.             break;
  245.       }
  246.    }
  247.    if (cnt < 0)
  248.       terminate (BADIO, &infile, &outfile, "I/O error on input");
  249.  
  250.    if (!done)
  251.       terminate (NOEND, &infile, &outfile, "no END_PLOT encountered.\n");
  252.  
  253.    if (!closein(&infile))
  254.       terminate (BADIO, &infile, &outfile, "can't close input file");
  255.  
  256.    if (!closeout(&outfile))
  257.       terminate (BADIO, &infile, &outfile, "can't close output file");
  258.  
  259.    exit (ALLOK);
  260. }
  261.